package DAO;

import POJO.User;

import java.sql.*;



public class UserDAO {
    /**
     * 数据库链接
     * @return SQL.Connection 类对象
     * @throws SQLException
     */
    public Connection getconn() throws SQLException {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println("UserDAO：JVM查找并加载指定的类失败");
            e.printStackTrace();
        }

        String url="jdbc:mysql://localhost:3306/db1?characterEncoding=utf8";
        String username="root";
        String password="021218";
        Connection connection;
        connection = DriverManager.getConnection(url, username, password);
        return connection;
    }

    /**
     * 用户登录DAO
     * @param username
     * @return POJO.User类对象
     * @throws SQLException
     */
//    public User login(String username) throws SQLException {
//        Connection conn = getconn();
//
//    }

    /**
     * 用户注册DAO
     * @param username 用户名
     * @param password 密码哈希值
     * @return 用户uid
     * @throws SQLException
     */
    public int reg(String username,String password) throws SQLException {
        Connection conn = getconn();
        String sql = "SELECT COUNT(*) total FROM db_user";
        ResultSet set = conn.createStatement().executeQuery(sql);
        set.next();
        int uid = set.getInt("total");

        sql="INSERT INTO db_user VALUES(?,?,'xxxxx','xxxxxxx','xxxxxxxx','D:/IDEA/Project/workersHome/web/headPhoto/headers/default.jpg')";
        PreparedStatement pst = conn.prepareStatement(sql);
        pst.setInt(1,uid + 1);
        pst.setString(2,username);
        pst.executeUpdate();

        sql = "INSERT INTO db_pwd VALUES(?,?)";
        pst = conn.prepareStatement(sql);
        pst.setInt(1,uid + 1);
        pst.setString(2,password);
        pst.executeUpdate();
        pst.close();
        conn.close();
        return uid + 1;
    }

    /**
     * 检查用户名是否存在
     * @param username 用户名
     * @return 是否存在布尔值
     * @throws SQLException
     */
    public boolean userNameExisted(String username) throws SQLException{
        Connection conn = getconn();
        String sql = "SELECT username  from db_user where username = ? limit 1";
        PreparedStatement pst = conn.prepareStatement(sql);
        pst.setString(1,username);
        ResultSet set = pst.executeQuery();
        if(set.next()){
            pst.close();
            conn.close();
            return true;
        }else{
            pst.close();
            conn.close();
            return false;
        }
    }

    public boolean userIdExisted(int userId) throws SQLException{
        Connection conn = getconn();
        String sql = "SELECT *  from db_user where userId = ? limit 1";
        PreparedStatement pst = conn.prepareStatement(sql);
        pst.setInt(1,userId);
        ResultSet set = pst.executeQuery();
        if(set.next()){
            pst.close();
            conn.close();
            return true;
        }else{
            pst.close();
            conn.close();
            return false;
        }
    }

    public void changeUserInfo(int userId, String perSign, String regPhone, String regEmail, String regPhoto) throws SQLException{
        Connection conn = getconn();
        String sql = "UPDATE db_user set perSign = ?, regPhone = ?, regEmail = ?, regPhoto = ? WHERE userId = ?";
        PreparedStatement pst = conn.prepareStatement(sql);
        pst.setString(1,perSign);
        pst.setString(2,regPhone);
        pst.setString(3,regEmail);
        pst.setString(4,regPhoto);
        pst.setInt(5,userId);
        pst.executeUpdate();
        pst.close();
        conn.close();


    }

}
